package org.lanqiao.sjx.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.lanqiao.bean.User;
import org.lanqiao.sjx.dao.UserDao;
import org.lanqiao.tools.BaseDao;

public class UserImpl extends BaseDao implements UserDao{

	@Override
//	用户使用电话和密码注册后的个人信息更新
	public Integer updateUser(User info) {
		// TODO Auto-generated method stub
		String sql="update user set name=?,sex=?,email=?,telphone=?,workTime=?,HeduBackground=?,workNtype=?,date=?,state=2 where id=?";
		try {
			return query.update(sql,info.getName(),info.getSex(),info.getEmail(),info.getTelphone(),
					info.getWorkTime(),info.getHeduBackground(),info.getWorkNtype(),info.getDate(),info.getId());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	
//	@Override
////	用户使用邮箱和密码注册后的个人信息更新
//	public Integer updateUserByEmail(User info) {
//		// TODO Auto-generated method stub
//		String sql="update user set name=?,sex=?,telphone=?,workTime=?,HeduBackground=?,workNtype=?,date=? where id=?";
//		try {
//			return query.update(sql,info.getName(),info.getSex(),info.getTelphone(),
//					info.getWorkTime(),info.getHeduBackground(),info.getWorkNtype(),info.getDate(),info.getId());
//		} catch (SQLException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		return null;
//	}
	
	@Override
	public User selectUser(Integer id) {
		// TODO Auto-generated method stub
		String sql="select * from user where id=?";
		try {
			return query.query(sql,new BeanHandler<User>(User.class),id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}


//	用户使用邮箱和密码进行登录
//	public Integer selectUserByEmailPas(String email, String password) {
//		// TODO Auto-generated method stub
//		String sql="select count(1) from user where email=? and password=?";
//		try {
//		Object query2= query.query(sql, new ScalarHandler<Integer>(),email,password);
//		return Integer.parseInt(query2.toString());
//		} catch (SQLException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		return -1;
//	}

	@Override
//	用户登录
	public User selectUserByb1Pwd(String b1, String password) {
		// TODO Auto-generated method stub
		String sql="select * from user where b1=? and password=?";
		try {
			 return query.query(sql,new BeanHandler<User>(User.class),b1,password);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	//	注册
	@Override
	public Integer insertUser(String b1,String password) {
		// TODO Auto-generated method stub
		String sql="insert into user (password,b1,state,date) values (?,?,1,Now())";
		try {
			return query.update(sql,password,b1);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
			return null;
	}

//	@Override
//	//	根据用户的电话和邮箱注册
//	public Integer insertUserByEmail(String email,String password) {
//		String sql="insert into user (password,email) values (?,?)";
//		try {
//			return query.update(sql,password,email);
//		} catch (SQLException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//			return null;
//	}


	@Override
	public List<User> selectALL() {
		String sql="select * from user";
		// TODO Auto-generated method stub
		try {
			return query.query(sql, new BeanListHandler<User>(User.class));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	
}
